import {createSlice} from "@reduxjs/toolkit";
import {useGetOneEquipmentById,useGetAllEquipmentListAPI} from "/src/apis/equipmentAPI.jsx";

const equipmentStore=createSlice({

    name:'equipment',
    initialState:{
        equipmentList:[],
        equipment:{}
    },

    reducers:{
        // 分页获取到所有设备
        getAllEquipmentList(state,action){
            state.equipmentList=action.payload;
        },

        // 根据Id获取一个设备数据
        getOneById(state,action){
            state.equipment=action.payload;
        }
    }
})

const {getAllEquipmentList,getOneById}=equipmentStore.actions;

// 分页获取equipmentList

const fetchGetAllEquipmentListByPage=(current,pageSize)=>{
    return async (dispatch)=>{
        const res=await useGetAllEquipmentListAPI( current,pageSize)
        dispatch(getAllEquipmentList(res.data))
    }
}

const fetchGetOneEquipmentById=(id)=>{
    return async (dispatch)=>{
        const res=await useGetOneEquipmentById(id)
        dispatch(getOneById(res.data))
    }
}

const equipmentReducer=equipmentStore.reducer;

export { getAllEquipmentList,fetchGetAllEquipmentListByPage ,getOneById,fetchGetOneEquipmentById }

export default equipmentReducer;

